<?php

namespace app\admin\controller;

use app\model\Teacher as T;
class Teacher extends AdminBase
{

    // CREATE TABLE `teacher` (
//   `id` int(11) NOT NULL AUTO_INCREMENT,
//   `created_at` int(11) NOT NULL COMMENT '创建时间',
//   `updated_at` int(11) NOT NULL COMMENT '更新时间',
//   `name` varchar(255) NOT NULL COMMENT '姓名' DEFAULT '',
//   `avatar` varchar(255) NOT NULL COMMENT '头像' DEFAULT '',
//   `content` text NOT NULL COMMENT '内容',
//   `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '状态 1正常 0禁用',
//   `weight` int(11) NOT NULL DEFAULT '0' COMMENT '权重',
//   PRIMARY KEY (`id`)
// ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='教师';

    private $pname = 'name/s';
    private $pdesc = 'desc/s';
    private $pmotto = 'motto/s';
    private $pavatar = 'avatar/s';
    private $pstatus = 'status/d';
    private $pweight = 'weight/d';
    private $pic = 'pic/s';


    public function index(string $do = '')
    {
        if ($do == 'json') {
            $where = [];
            $kw  = $this->request->param('kw');
            if ($kw) $where[] = ['name','like','%'.$kw.'%'];
            $list = (new T())->listQuery($where);
            $list = $list->toArray();
            return $this->returnMsg($list,code: 0);
        }
        $this->assign([ 'limit'=>10]); 
        return $this->fetch();
    }

    public function bedit(string $do = '')
    {
        $d = $this->only($do ? ['@token'=>'','id/d/ID参数错误','av','af'] : ['@token'=>'','id/d/ID参数错误', $this->pname,$this->pavatar,$this->pdesc,$this->pmotto,$this->pstatus,$this->pweight,$this->pic]);
        $id = $d['id'];
        $Myobj = T::get("id = $id");
        if(!$Myobj) return $this->returnMsg("数据不存在");
        if ($do == 'up') {
            $value = $d['av'];
            $field = $d['af'];
            if(!in_array($field,['name','avatar','desc','motto','status','weight','pic'])) return $this->returnMsg("参数错误");
            if($field == 'avatar') $this->only([$this->pavatar]);
            if($field == 'pic') $this->only([$this->pic]);
            if($Myobj->save([$field=>$value])){
                return $this->returnMsg("设置成功", code: 1);
            }
        }else{
            if($Myobj->save($d)){
                return $this->returnMsg("编辑成功", code: 1);
            }
        }
    }

    //badd
    public function badd()
    {
        $d = $this->only(['@token'=>'id/d/ID参数错误',$this->pname,$this->pavatar,$this->pic,$this->pdesc,$this->pmotto,$this->pstatus,$this->pweight]);
        if((new T())->save($d)){
            return $this->returnMsg("添加成功", code: 1);
        }else{
            return $this->returnMsg("添加失败");
        }
    }

    //bdel 删除
    public function bdel()
    {
            $id = $this->only(['@token'=>'','id'])['id'];
            $id = is_array($id) ? implode(',',$id) : $id;
            if(!$id) return $this->returnMsg('参数错误');
            $ids = explode(',', $id);
            if((new T())->where('id','in',$id)->delete()){
                return $this->returnMsg("删除成功", code: 1);
            }else{
                return $this->returnMsg("删除失败");
            }
    }

}
